package com.tencent._05_tran;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建消费者
        TransactionMQProducer producer = new TransactionMQProducer("producer_tran_group");
        // 设置链接
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 设置监听器
        producer.setTransactionListener(new MyTranListener());
        // 启动
        producer.start();
        // 创建消息
        Message message = new Message("tran_topic", "tran_tags", "我是事务消息".getBytes(StandardCharsets.UTF_8));
        // 传消息
        TransactionSendResult sendResult = producer.sendMessageInTransaction(message, "我就是o");
        // 判断是否成功
        System.out.println(sendResult);
        // 关闭
        producer.shutdown();
    }
}
